package com.xiaoxie.service.impl;

import com.xiaoxie.DTO.UserDTO;
import com.xiaoxie.entity.User;
import com.xiaoxie.mapper.UserDao;
import com.xiaoxie.mapper.WordDao;
import com.xiaoxie.mapper.learnLogDao;
import com.xiaoxie.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小谢
 * @since 2023-10-13
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private WordDao wordDao;
    @Autowired
    private learnLogDao learnLogDao;

    /**
     * 更新用户状态
     *
     * @param account
     */
    @Override
    public boolean updateFinish( String account) {
        if (wordDao.getNeedMemory(account).isEmpty()){
            userDao.updateFinish(account);
            return true;
        }
        return false;
    }

    /**
     * 用户登录
     * @param userDTO
     * @return
     */
    @Override
    public User Login(UserDTO userDTO) {
        return userDao.login(userDTO);
    }

    @Override
    public void initial(User user) {
        //初始化用户数据
        userDao.init(user.getUserAccount());
        User select = userDao.select(user.getUserAccount());
        //初始化单词记忆数据，此处需要在用户登录时初始化
        if (select.getLearnFinish().equals("false") && select.getWordInit().equals("false")) {
            wordDao.initWrong(user.getUserAccount());
            userDao.updateWordInit(user.getUserAccount());
            //初始化学习记录
            learnLogDao.insert(user.getId(),wordDao.getNeedMemory(user.getUserAccount()).size());
        }

    }

    @Override
    public boolean insertUser(String account, String password) {
        User user = userDao.select(account);
        if (user!=null){
            return false;
        }
        userDao.enroll(UUID.randomUUID().hashCode(),account, password);
        return true;
    }
}
